package com.nuaa.yygh.cmn.controller;

import com.nuaa.model.cmn.Dict;
import com.nuaa.yygh.cmn.service.DictService;
import com.nuaa.yygh.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    //controller是用去调用service中的方法的
    @ApiOperation(value = "根据数据id查询子数据")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    //导出数据字典接口
    @ApiOperation(value = "导出数据")
    @GetMapping("exportData")
    public  void exportDict(HttpServletResponse response){
        dictService.exportData(response);
    }

    //导入数据字典
    @ApiOperation(value = "导入数据")
    @PostMapping("importData")
    public Result importDict(MultipartFile file){
        dictService.improtData(file);
        return Result.ok();
    }

    //为了给hosp模块调用，也就是医院管理,通过两个参数获取
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value){
        return dictService.getNameByDictCodeAndValue(dictCode,value);
    }

    //跟上面一样不过只需一个参数获取
    @GetMapping(value = "getName/{value}")
    public String getName(@PathVariable("value") String value){
        return dictService.getNameByDictCodeAndValue("",value);
    }

    //根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }



}
